package com.yango.teach.controller;


import com.yango.commonutil.result.Result;
import com.yango.teach.entity.Chapter;
import com.yango.teach.entity.vo.course.ChapterVo;
import com.yango.teach.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程章节 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Api(description = "课程章节 前端控制器")
@RestController
@RequestMapping("/teach/chapter")
//@CrossOrigin
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    //课程章节小节列表-嵌套
    @ApiOperation(value = "课程章节小节列表-嵌套")
    @GetMapping("/getChapterVideoByCourseId/{courseId}")
    public Result getChapterVideoByCourseId(@ApiParam(name = "courseId", value = "课程id", required = true)
                                            @PathVariable String courseId) {
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("chapterVoList", chapterVoList);
    }

    //增加章节
    @ApiOperation(value = "添加章节")
    @PostMapping("/saveChapter")
    public Result saveChapter(@ApiParam(name = "chapter", value = "章节对象", required = true)
                              @RequestBody Chapter chapter) {
        boolean save = chapterService.save(chapter);
        if (save) {
            return Result.ok().message("添加章节成功!");
        } else {
            return Result.error().message("添加章节失败!");
        }
    }

    //根据id查询章节
    @ApiOperation(value = "根据id查询章节")
    @GetMapping("/getChapterById/{id}")
    public Result getChapterById(@ApiParam(name = "id", value = "章节id", required = true)
                                 @PathVariable String id) {
        Chapter chapter = chapterService.getById(id);
        if (chapter == null) {
            return Result.error().message("没有查询到章节信息!");
        } else {
            return Result.ok().data("chapter", chapter);
        }
    }

    //根据章节id修改章节信息
    @ApiOperation(value = "根据章节id修改章节信息")
    @PutMapping("/updateChapter/{id}")
    public Result updateChapter(@ApiParam(name = "id", value = "章节id", required = true)
                                @PathVariable String id,
                                @ApiParam(name = "chapter", value = "章节对象", required = true)
                                @RequestBody Chapter chapter) {
        //调用方法修改
        chapter.setId(id);
        boolean flag = chapterService.updateById(chapter);
        if (flag) {
            return Result.ok().message("更新章节成功!");
        } else {
            return Result.error().message("更新章节失败!");
        }
    }

    //删除章节
    @ApiOperation(value = "删除章节")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(name = "id", value = "章节id", required = true)
                             @PathVariable String id) {
        boolean flag = chapterService.removeChapterById(id);
        if (flag) {
            return Result.ok().message("删除章节成功!");
        } else {
            return Result.error().message("删除章节失败!");
        }
    }

}

